﻿using System;
using System.Linq;
using Yyyg.Models;

namespace Yyyg.BLL
{
    public class MemberService : TableServiceBase<Member>, IBLL.IMemberService
    {
        LuckyEntities db;

        public MemberService(LuckyEntities db) : base(db)
        {
            this.db = db;
        }

        /// <summary>
        /// 检测账户是否已经存在
        /// </summary>
        /// <param name="email">邮箱</param>
        public bool HasUser(string acc)
        {
            return db.Member.Any(s => s.Email == acc || s.Mobile == acc);
        }

        /// <summary>
        /// 检测是否包含该账号且密码正确
        /// </summary>
        /// <param name="acc">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        /// <returns></returns>
        public bool LoginCheck(string emailOrMob, string pwd)
        {
            return db.Member.Any(s => (s.Email == emailOrMob || s.Mobile == emailOrMob) && s.Pwd == pwd);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="m">用户信息</param>
        /// <param name="headimg"></param>
        public Member AddUser(string mob, string pwd)
        {
            if (string.IsNullOrEmpty(mob)) throw new ArgumentNullException("mob", "电话不能为空");
            if (string.IsNullOrEmpty(pwd)) throw new ArgumentNullException("pwd", "密码不能为空");
            if (HasUser(mob)) throw new ApplicationException("该用户已注册");
            var m = db.Member.Add(new Member()
            {
                Acbal = 0,
                Petname = "用户" + mob,
                Mobile = mob,
                Pwd = pwd,
                Email = "",
                Createtime = DateTime.Now,
                LastMyygTime = DateTime.Now,
                Sex = "男",
            });
            db.SaveChanges();
            return m;
        }

        /// <summary>
        /// 返回单个用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public Member GetUser(int uid)
        {

            return db.Member.FirstOrDefault(f => f.Uid == uid);

        }

        public int Count()
        {
            return db.Member.Count();
        }

        /// <summary>
        /// 根据账号查询用户
        /// </summary>
        /// <param name="acc"></param>
        /// <returns></returns>
        public Member GetUser(string acc)
        {
            return db.Member.FirstOrDefault(s => s.Mobile == acc || s.Email == acc);
        }

        /// <summary>
        /// 将商品添加到用户的收藏夹
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public bool AddFavorites(int uid, int gid)
        {
            var user = GetUser(uid);
            if (user.UserCollect.Any(a => a.Gid == gid)) return false;
            var col = new UserCollect();
            col.CreateTime = DateTime.Now;
            col.Uid = uid;
            col.Gid = gid;
            user.UserCollect.Add(col);
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 将商品从用户收藏中删除
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="gid"></param>
        /// <returns></returns>
        public bool CancelFavorites(int uid, int gid)
        {
            var user = GetUser(uid);
            var col = db.UserCollect.FirstOrDefault(f => f.Gid == gid);
            if (col == null) return false;
            db.UserCollect.Remove(col);
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 更新用户上次访问我的云购时间
        /// </summary>
        /// <param name="uid"></param>
        public void UpdateLastMyygTime(int uid)
        {
            var user = GetUser(uid);
            user.LastMyygTime = DateTime.Now;
            db.SaveChanges();
        }

        /// <summary>
        /// 检测是否含有未查看的开奖项
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool HasNotViewed(int uid)
        {
            var user = GetUser(uid);
            return db.Rushby_over.
                Where(
                w => w.Createtime > user.LastMyygTime
                && db.Orders.Where(w1 => w1.Uid == uid).Select(s => s.Rush_id).Contains(w.Rush_id)
                ).Count() > 0;
        }
    }
}
